package com.iss.lxhproject.controller;

import com.iss.lxhproject.entity.Room;
import com.iss.lxhproject.model.MyResponseData;
import com.iss.lxhproject.service.RoomfreeService;
import com.iss.lxhproject.service.RoomfulService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("room/")
public class RoomController {
    @Autowired
    private RoomfreeService roomfreeService;
    @Autowired
    private RoomfulService roomfulService;
    //查询全部的空闲房间
    @RequestMapping("roomfree")
    public List<Room> selectAllroomfree(Double priceFrom,Double priceTo){
        List<Room> list= roomfreeService.selectAllRooms(priceFrom,priceTo);
        //System.out.println(list);
        return list;
    }
    //查询所有的满房间
    @RequestMapping("roomful")
    public List<Room> selectAllroomful(){
        List<Room> list = roomfulService.selectAllRooms();
        return list;
    }
    //删除空闲房间
    @RequestMapping("deleteOneRoomfree")
    public MyResponseData deleteOneRoomfree(String[] room_ids){
        //System.out.println(room_id.length);

        MyResponseData responseData = new MyResponseData();
        try {
            boolean flag = roomfreeService.deleteRoom(room_ids);
            System.out.println(flag);

            if (flag) {
                responseData.setErrorCode(0);
                responseData.setStatus("删除成功");
            }
        } catch (Exception e){
            responseData.setErrorCode(-1);
            responseData.setStatus("请选择要清除的房间信息");
        }
        return responseData;
    }

    //更新空闲的房间
    @RequestMapping("updateRoomfree")
    public MyResponseData updateRoomfree(Room room){
        MyResponseData responseData = new MyResponseData();
        boolean flag = roomfreeService.updatetRoom(room);
        System.out.println(flag);
        if(flag){
            responseData.setErrorCode(0);
            responseData.setObject(room);
            responseData.setStatus("更新成功");
        }else {
            responseData.setErrorCode(-1);
            responseData.setStatus("更新失败");
        }
        return responseData;
    }
    @RequestMapping("selectRoomfreeById")
    public MyResponseData selectRoomfreeById(String room_id){
        MyResponseData responseData = new MyResponseData();
       Room rooms = roomfreeService.selectRoomfreeById(room_id);
       responseData.setErrorCode(0);
       responseData.setStatus("success");
       responseData.setObject(rooms);
       return responseData;
    }
    @RequestMapping("InsertRoomfree")
    public MyResponseData InsertRoomfree(Room room){
        MyResponseData responseData = new MyResponseData();
        Room roomid = roomfreeService.selectRoomfreeById(room.getRoom_id());
        System.out.println("roomid="+roomid);
        try {
            if (roomid == null) {
                Integer count = roomfreeService.insertRoom(room);
                System.out.println("count="+count);
                if (count > 0) {
                    responseData.setErrorCode(0);
                    responseData.setStatus("插入成功");
                    responseData.setObject(room);
                } else {
                    responseData.setErrorCode(-1);
                    responseData.setStatus("插入失败");
                }
            } else {
                responseData.setErrorCode(-2);
                responseData.setStatus("房间名已存在");
            }
        }catch (Exception e){
            responseData.setErrorCode(-3);
            responseData.setStatus("请完善未填写的信息");
        }
        return responseData;
    }


}
